/*******************************************************************************
02_10: Make Table 10: Effect of years since migration (YSM) on non-market work
	for immigrant couples migrating together

Replication code for "Culture and gender allocation of tasks: source country 
	characteristics and the division of non-market work among US immigrants" in
	Review of Economics of the Household

Authors: Francine Blau, Lawrence Kahn, Matthew Comey, Amanda Eng, Pamela 
	Meyerhofer, and Alexander Willen
	
Date: 10/16/2020

Description: This code generates Table 10

*******************************************************************************/


/*******************************************************************************

I. Set labels and controls

*******************************************************************************/

***A. Set table labels
putexcel set "$tables/Table_10", replace

putexcel A1="Table 10: Effect of years since migration (YSM) on non-market work for immigrant couples migrating together", bold

putexcel A2="Panel A. All Women", italic
putexcel E2="Panel B. All Men", italic
putexcel A10="Panel C. Women with Children", italic
putexcel E10="Panel D. Men with Children", italic

putexcel A3="YSM"
putexcel B3="Total Non-Market Work"
putexcel C3="Housework"
putexcel D3="Childcare"
putexcel E3="YSM"
putexcel F3="Total Non-Market Work"
putexcel G3="Housework"
putexcel H3="Childcare"
putexcel A11="YSM"
putexcel B11="Total Non-Market Work"
putexcel C11="Housework"
putexcel D11="Childcare"
putexcel E11="YSM"
putexcel F11="Total Non-Market Work"
putexcel G11="Housework"
putexcel H11="Childcare"


***B. Set control variables
local ysm_controls yrsusa1 yrsusa2 part_yrsusa1 part_yrsusa2

local controls age age2 RACE2 RACE3 RACE4 RACE5 cohort70_79 cohort80_89 cohort90_99 cohort00_09 cohort10_17

local fe i.day i.month i.year i.state

local ecp_controls hsdi scol cold young_children middle_children old_children part_age part_age2 part_hsdi part_scol part_cold part_RACE2 part_RACE3 part_RACE4 part_RACE5 part_immig part_secondgen part_cohort70_79 part_cohort80_89 part_cohort90_99 part_cohort00_09 part_cohort10_17

local wife_work wifehours wifehours_vary


/*******************************************************************************

II. Fill in Table

*******************************************************************************/

**Loop through table panels
foreach panel in A B C D {
	
	*Set sample restrictions and labels
	preserve
	
	if "`panel'" == "A" {
		di "*****Regressing for All Immigrant Women*****"
		
		keep if female == 1 & immig==1
		
		local col_ysm "A"
		local col_$nmw "B"
		local col_$hwork "C"
		local col_$ccare "D"
		
		local top_row1 = 4
		local top_row2 = 5
		}
		
	else if "`panel'" == "B" {
		di "*****Regressing for All Immigrant Men*****"
		
		keep if female == 0 & immig==1
				
		local col_ysm "E"
		local col_$nmw "F"
		local col_$hwork "G"
		local col_$ccare "H"
		
		local top_row1 = 4
		local top_row2 = 5
		}
		
	else if "`panel'" == "C" {
		di "*****Regressing for Immigrant Women with Children*****"
		
		drop if hh_numownkids== 0
		keep if youngc<18 
		keep if female == 1 & immig==1
				
		local col_ysm "A"
		local col_$nmw "B"
		local col_$hwork "C"
		local col_$ccare "D"
		
		local top_row1 = 12
		local top_row2 = 13
		}
		
	else if "`panel'" == "D" {
		di "*****Regressing for Immigrant Men with Children*****"
		
		drop if hh_numownkids== 0
		keep if youngc<18 
		keep if female == 0 & immig==1
				
		local col_ysm "E"
		local col_$nmw "F"
		local col_$hwork "G"
		local col_$ccare "H"
		
		local top_row1 = 12
		local top_row2 = 13
		}


	**Loop through non-market work variables
	foreach var in $nmw $hwork $ccare {
	    
		local row1 = `top_row1'
		local row2 = `top_row2'
	    
		reg `var' `ysm_controls' GGI GDP_000 fertility `controls' `ecp_controls' `fe' [aw=newwt], robust cluster(bpl) 
		local dof = e(df_r)
		
		*Save coefficient matrix: a row vector with YSM, YSM^2, partner YS, and partner YSM^2, in that order, then all the other variables
		matrix `var'beta_`panel' = e(b)
				
		*Save coefficient covariance matrix
		matrix `var'cov_`panel' = e(V)
		
		*Loop through years since migration
		forval ysm = 10(10)30 {
		    
			local ysm2 = `ysm'^2
			
			*Insert YSM
			putexcel `col_ysm'`row1' = `ysm'
			
			*Get effect
			local est = (`ysm'*`var'beta_`panel'[1,1]) + (`ysm2'*`var'beta_`panel'[1,2]) + (`ysm'*`var'beta_`panel'[1,3]) + (`ysm2'*`var'beta_`panel'[1,4])
			
			*Get standard error
			local ste = sqrt( (`ysm'*`ysm'*`var'cov_`panel'[1,1]) ///
				+ (`ysm'*`ysm2'*`var'cov_`panel'[1,2]) ///
				+ (`ysm'*`ysm'*`var'cov_`panel'[1,3]) ///
				+ (`ysm'*`ysm2'*`var'cov_`panel'[1,4]) ///
				+ (`ysm'*`ysm2'*`var'cov_`panel'[2,1]) ///
				+ (`ysm2'*`ysm2'*`var'cov_`panel'[2,2]) ///
				+ (`ysm2'*`ysm'*`var'cov_`panel'[2,3]) ///
				+ (`ysm2'*`ysm2'*`var'cov_`panel'[2,4]) ///
				+ (`ysm'*`ysm'*`var'cov_`panel'[3,1]) ///
				+ (`ysm'*`ysm2'*`var'cov_`panel'[3,2]) ///
				+ (`ysm'*`ysm'*`var'cov_`panel'[3,3]) ///
				+ (`ysm'*`ysm2'*`var'cov_`panel'[3,4]) ///
				+ (`ysm'*`ysm2'*`var'cov_`panel'[4,1]) ///
				+ (`ysm2'*`ysm2'*`var'cov_`panel'[4,2]) ///
				+ (`ysm2'*`ysm'*`var'cov_`panel'[4,3]) ///
				+ (`ysm2'*`ysm2'*`var'cov_`panel'[4,4]) )
			
				
			*Add to table
			getstars `est' `ste' `dof'
			putexcel `col_`var''`row1' = (r(eststar))
			putexcel `col_`var''`row2' = `ste', nformat("(0.000)")

			
			*Iterate rows
			local row1 = `row1' + 2
			local row2 = `row2' + 2
				
			
			
			}
			
			
		}
		
	restore
	
	}
	